package com.lottery.ruok.lottrey.ui.component;

import android.content.Context;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.adapter.LHCLeftMenuAdapter;
import com.lottery.ruok.lottrey.adapter.recyclleradapter.RecyclerItemClickListener;
import com.lottery.ruok.lottrey.bean.LHCLeftModel;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class LHCLeftMenuView extends BaseRelativeLayout {
    @BindView(R.id.view_recycler)
    RecyclerView temaList;
    private List<LHCLeftModel> data;
    private LHCLeftMenuAdapter lhcOptionAdapter;
    private OnLHCLeftClickListener onLHCLeftClickListener;


    public LHCLeftMenuView(Context context) {
        super(context);
    }

    public LHCLeftMenuView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public LHCLeftMenuView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void init(Context context, AttributeSet attrs, int defStyleAttr) {
        String[] options = {"单", "双", "大", "小", "0头", "1头", "2头", "3头", "4头", "红", "绿", "蓝"};
        data = new ArrayList<>();
        for (String text : options) {
            LHCLeftModel lhcLeftModel = new LHCLeftModel();
            lhcLeftModel.setName(text);
            lhcLeftModel.setSelected(false);
            data.add(lhcLeftModel);
        }
    }

    @Override
    protected void initView(Context context) {
        setContentView(R.layout.view_lhc_left_menu);
        lhcOptionAdapter = new LHCLeftMenuAdapter(mContext, data);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        temaList.setLayoutManager(linearLayoutManager);
        temaList.setAdapter(lhcOptionAdapter);
        temaList.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL));
        temaList.addOnItemTouchListener(new RecyclerItemClickListener(mContext) {
            @Override
            protected boolean onItemClick(View view, int position) {
                LHCLeftModel item = lhcOptionAdapter.getList().get(position);
                clearSelected();
                item.setSelected(true);
                lhcOptionAdapter.notifyDataSetChanged();
                if (onLHCLeftClickListener != null) {
                    onLHCLeftClickListener.onClick(item.getName());
                }
                return true;
            }
        });
    }

    private void clearSelected() {
        if (data != null) {
            for (LHCLeftModel item : data) {
                item.setSelected(false);
            }
        }
    }

    @Override
    protected void load(Context context) {

    }

    public void setOnLHCLeftClickListener(OnLHCLeftClickListener onLHCLeftClickListener) {
        this.onLHCLeftClickListener = onLHCLeftClickListener;
    }

    public interface OnLHCLeftClickListener {
        void onClick(String text);
    }
}
